package com.example.springbootdemo.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.locationtech.jts.geom.Point;

import java.io.IOException;

public class PointToJsonSerializer extends JsonSerializer<Point> {
    @Override
    public void serialize(Point value, JsonGenerator jgen,
                          SerializerProvider provider) throws IOException,
            JsonProcessingException {

        double[] jsonArray = new double[2];
        try
        {
            if(value != null) {
                double lat = value.getY();
                double lon = value.getX();
                jsonArray[0] = lon;
                jsonArray[1] = lat;
            }
        }
        catch(Exception e) {}

        jgen.writeArray(jsonArray, 0, 2);
    }
}
